package campus.aiit.ssl.controller;

import campus.aiit.ssl.domain.model.Block;
import campus.aiit.ssl.service.BlockService;
import campus.aiit.ssl.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;

@Api(description = "区域块相关api")
@RestController
public class BlockController {
    @Resource
    private BlockService blockService;
    @Resource
    private UserService userService;

    @ApiOperation("通过区域块ID删除区域块")
    @DeleteMapping("block")
    public int deleteBlock(Long id) {
        return blockService.deleteByPrimaryKey(id);
    }

    @ApiOperation("添加区域块")
    @PostMapping("block")
    public int insertBlock(@RequestBody Block block) {
        return blockService.insertBlock(block);
    }

    @ApiOperation("通过区域块Id查区域块")
    @GetMapping("block")
    public Block[] selectBlock(Long id) {
        return blockService.selectByPrimaryKey(id);
    }

    @ApiOperation("修改区域块")
    @PutMapping("block")
    public int updateBlock(@RequestBody Block block) {
        return blockService.updateByPrimaryKeySelective(block);
    }

    @ApiOperation("(block/getBlocks)获取区域块/(block/getBlocks/{userId})获取该用户管理区域块与没人管理的区域块")
    @GetMapping({
            "block/getBlocks",
            "block/getBlocks/{userId}"
    })
    public Block[] getBlocks(@PathVariable(required = false) String userId){
        return blockService.getBlocks(userId);
    }

    @ApiOperation("获取没人员管理的区域块")
    @GetMapping("block/getBlocks/nullUser")
    public Block[] getBlockWithNullUser(){
        return blockService.blockWithUserNull();
    }

    @ApiOperation("为区域块绑定房间")
    @PostMapping("block/addRoomForBlock")
    public int addRoomForBlock(@RequestBody Map map) {
        return blockService.addRoomForBlock(map);
    }

    @ApiOperation("移除区域块绑定的房间")
    @PostMapping("block/delRoomForBlock")
    public int delRoomForBlock(@RequestBody Map map) {
        return blockService.delRoomForBlock(map);
    }

    @ApiOperation("通过userId获取区域块")
    @GetMapping("block/getBlockByUserId")
    public Block[] getBlockByUserId(String userId) {
        int roleId = userService.getUserById(userId).getRoleId();
        if(roleId == 0){
            userId = null;
        }
        return blockService.getBlockByUserId(userId);
    }


}

